'''
    用于读取url.csv文件，从而获取到图片下载地址
'''
import time

import requests
import json
import asyncio
headers = {
    'Cookie':'SINAGLOBAL=618919066387.7864.1624346401618; UOR=ilive.lenovo.com.cn,s.weibo.com,www.baidu.com; ULV=1715763314391:65:1:1:1143349720519.5286.1715763314389:1712387997847; ALF=1718355336; SUB=_2A25LQAjYDeRhGeBM7FMY9y_MzDqIHXVoPAQQrDV8PUJbkNANLUqtkW1NRNyWoXjoHBcQOlhuypndlHGIPpo7L3Ii; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WhTNAlv6EWJAXoS..AAr1Kh5JpX5KzhUgL.FoqES024S027S0q2dJLoI0qLxKBLB.zL1KqLxK-L12qLB-2LxKqL1KnLB-qLxKqL1-eL1h.LxKML1hzLBo.LxK-L1K2L1hqt; XSRF-TOKEN=kx5jb_9t1vvvNGwwNr6Kr188; WBPSESS=03Rwz19Z2C7dvXKIo40JJUonpHzzhP1JQuWdfXlDkW7M72rIIe3Dsnpc0CcHRu13qK4emuJBNXOZeSpXXJHkriVyy1YKZEonDTSo-rjRhqg4xNRJVtuC4E3udilWgTTC1Uf8Urb2BMmsXPp9XVJeNg==',
    'Referer':'https://weibo.com/u/7744301004?tabtype=album',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0'
}
# url = 'https://weibo.com/ajax/profile/getImageWall?uid=7744301004&sinceid=0&has_album=true'
# resp = requests.get(url,headers=headers)
# print(resp)
def read_url():
    dic = []
    with open('爬取文件/url.csv',mode='r') as file:
        for url in file:
            url = url.strip()
            # print(item)
            resp = requests.get(url, headers=headers)
            json_data = resp.json()
            album_list = json_data['data']['list']
            for album in album_list:
                yield album

def get_pid(base_url,res):
    offset = 0
    with open('爬取文件/img_url.csv', mode='w') as f:
        for album in res:
            pid = album['pid']
            img_url = base_url.format(pid)
            # print(img_url)
            f.write(img_url + '\n')
            offset = offset + 1
            print('url写入',offset)
        print('写入完成')
# 下载图片
def download_img():
    flag = 0
    with open('爬取文件/img_url.csv',mode='r') as f:
        for url in f:
            img_url = url.strip()
            # print(url)
            resp = requests.get(img_url,headers=headers)
            img = resp.content
            time.sleep(0.2)
            with open('爬取文件/img/{}.jpg'.format(flag),mode= 'wb') as file:
                file.write(img)
                flag = flag + 1
            print('爬取照片',flag)
        print('爬取完毕')

def main():
    base_url = 'https://wx3.sinaimg.cn/orj360/{}.jpg'
    # res = read_url()
    # get_pid(base_url,res)
    download_img()

if __name__ == '__main__':
    main()